package com.wing.cabinet.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.cabinet.dao.AuthorityMaterielDao;
import com.wing.cabinet.model.entity.AuthorityMateriel;
import com.wing.cabinet.model.request.AuthorityMaterielForm;
import com.wing.cabinet.model.response.AuthorityMaterielVO;
import com.wing.common.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 权限组限制物料表-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class AuthorityMaterielServiceImpl extends ServiceImpl<AuthorityMaterielDao, AuthorityMateriel> implements AuthorityMaterielService {

    @Override
    public int add(AuthorityMaterielForm authorityMaterielForm) {

        //校验

        AuthorityMateriel authorityMateriel = new AuthorityMateriel();
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(AuthorityMateriel.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(authorityMaterielForm, authorityMateriel, copyOptions);

        return baseMapper.insert(authorityMateriel);
    }

    @Override
    public List<AuthorityMaterielVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<AuthorityMaterielVO> pageList4app(Page page, Map<String, Object> params) {
        List<AuthorityMaterielVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public AuthorityMaterielVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }




    @Override
    public JsonResult updateList(List<AuthorityMaterielForm> list) {
        List<Long> ids = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId()!=null){//修改已经有的
                ids.add(list.get(i).getId());
                AuthorityMateriel authorityMateriel = new AuthorityMateriel();
                CopyOptions copyOptions = CopyOptions.create()
                        .setEditable(AuthorityMateriel.class)
                        .setIgnoreError(true)
                        .setIgnoreNullValue(true);
                BeanUtil.copyProperties(list.get(i), authorityMateriel, copyOptions);
                baseMapper.updateById(authorityMateriel);
            }else{//新添加的
                AuthorityMateriel authorityMateriel = new AuthorityMateriel();
                CopyOptions copyOptions = CopyOptions.create()
                        .setEditable(AuthorityMateriel.class)
                        .setIgnoreError(true)
                        .setIgnoreNullValue(true);
                BeanUtil.copyProperties(list.get(i), authorityMateriel, copyOptions);
                baseMapper.insert(authorityMateriel);
            }
        }
        //这个权限组已经有了的物料
        List<AuthorityMaterielVO> authorityMaterielVOList=baseMapper.list4app(new HashMap(){{put("manageId",list.get(0).getManageId());}});
        for (AuthorityMaterielVO authorityMaterielVO:authorityMaterielVOList){
            if (!ids.contains(authorityMaterielVO.getId())){
                //刪除沒有了的
                baseMapper.deleteById(authorityMaterielVO.getId());
            }
        }
        return JsonResult.success();
    }
}
